﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;
namespace IRIS.Controllers
{
    public class StaticPagesController : Controller
    {
        //
        // GET: /StaticPages/
        DataRepo m_db = new DataRepo();
        public ActionResult Index()
        {
            var model = m_db.getAllStaticPages();
            return View(model);
        }

        //
        // GET: /StaticPages/Details/5

        public ActionResult Details(int? id)
        {
            //skoða hvort að gilt gildi er sent inn
           
            if(id.HasValue)
            {
              
                
                StaticPageIECategoriesViewModel VM = new StaticPageIECategoriesViewModel { m_staticPage = m_db.getStaticById(id.Value), m_staticPages = m_db.getAllStaticPages() };   
                return View(VM);
            }
            return View("NotFound");
        }

        //
        // GET: /StaticPages/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /StaticPages/Create
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Create(StaticPage staticPage)
        {
            if(string.IsNullOrEmpty(staticPage.Text))
                ModelState.AddModelError("Text","Verður að skrifa við reit");
            if (string.IsNullOrEmpty(staticPage.Title))
                ModelState.AddModelError("Title", "Verður að skrifa við reit");
            
            try
            {
                if (ModelState.IsValid)
                {
                    staticPage.Modified = DateTime.Now;
                    staticPage.Created = DateTime.Now;
                    staticPage.Alive = true;
                    m_db.addStaticPage(staticPage);
                    return RedirectToAction("Index");
                }
                return View(staticPage);
            }
            catch
            {
                return View("Error");
            }
        }
        
        //
        // GET: /StaticPages/Edit/5
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Edit(int? id)
        {
            if (id.HasValue)
            {
                

                var model = m_db.getStaticById(id.Value);
                return View(model);
            }
            return View("Notfound");
        }

        //
        // POST: /StaticPages/Edit/5
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Edit(int id, StaticPage staticPage)
        {
            
            if (string.IsNullOrEmpty(staticPage.Text))
                ModelState.AddModelError("Text", "Verður að skrifa við reit");
            if (string.IsNullOrEmpty(staticPage.Title))
                ModelState.AddModelError("Title", "Verður að skrifa við reit");

            try
            {
                if(ModelState.IsValid)
                {
                    staticPage.Modified = DateTime.Now;
                    m_db.editStaticPage(id,staticPage);
                    return RedirectToAction("Index");
                }

                return View("Error");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /StaticPages/Delete/5
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Delete(int? id)
        {
            if (id.HasValue)
            {
                if(id.Value==7||id.Value==6)
                {
                    return View("NotFound");
                }
                var model = m_db.getStaticById(id.Value);
                return View(model);
            }
            return View("Notfound");
        }

        //
        // POST: /StaticPages/Delete/5
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Delete(int? id,StaticPage staticPage)
        {
            

            try
            {
                if (id.HasValue)
                {
                    staticPage.Alive = false;
                    m_db.deleteStaticPage(id.Value, staticPage);
                    return RedirectToAction("Index");
                }
                return View(staticPage);
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
